//
//  LXAddNewAddressViewController.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/8/16.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXAddNewAddressViewController.h"
#import "LXSwitchViewCell.h"
#import "LXTextFieldCell.h"

#import "AreaPickView.h"
static NSString *const LXSwitchViewCellID = @"LXSwitchViewCell";

@interface LXAddNewAddressViewController ()<UITableViewDelegate,UITableViewDataSource,LXTextFieldCellDelegate,AreaPickViewDelegate>

@property(nonatomic,weak) UITableView *tableView;

@property(nonatomic,strong)NSMutableDictionary * receiptAddressDic;

@end

@implementation LXAddNewAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.address = [[AddressModel alloc] init];
    [self setUpTableView];
}

- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.contentInset = UIEdgeInsetsMake(74, 0, 0, 0);
    tableView.rowHeight = MineRatio(60);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [self xw_registerBackInteractiveTransitionWithDirection:XWInteractiveTransitionGestureDirectionRight transitonBlock:^(CGPoint startPoint) {
        [self.navigationController popViewControllerAnimated:YES];
    } edgeSpacing:50];

}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
    UIButton * saveAddress = [UIButton buttonWithType:UIButtonTypeSystem];
    [saveAddress setTitle:@"保存" forState:UIControlStateNormal];
    [saveAddress setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveAddress.frame = CGRectMake(0, 0, 50, 30);

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveAddress];
    [saveAddress addTarget:self action:@selector(saveAddress:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)saveAddress:(UIButton*)button{
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    if([self.address fillComplete]){
        [[LXDataManager manager] addAddressWith:self.address complete:^(BOOL success, id res) {
            if(success){
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                    [self.view alertWithTitle:@"提示" mesege:@"信息有误请检查" buttons:@[@"好的"] handle:^(NSInteger buttonIndex) {
                        [self.view dismissAlert];
                    }];
                }
        }];
    }else{
        [self.view alertWithTitle:@"提示" mesege:@"信息不完整请完善" buttons:@[@"哦"] handle:^(NSInteger buttonIndex) {
            [self.view dismissAlert];
        }];
    }
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = @"新增收货地址";

}

-(void)backButtonClick:(UIButton*)aButton{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSArray * titles;
    if(!titles){
        titles =   @[@"收货人",@"联系电话",@"所在地址",@"请填写详细地址"];
    }
    if (indexPath.section == 1) {
        LXSwitchViewCell *switchViewCell = [tableView dequeueReusableCellWithIdentifier:LXSwitchViewCellID];
        if(!switchViewCell){
            switchViewCell = [[LXSwitchViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LXSwitchViewCellID];
        }
        switchViewCell.handleCallBack = ^(LXSwitchViewCell * cell){
            self.address.def_addr = [cell.sw isOn]?@"1":@"0";
        };
        switchViewCell.textLabel.text = @"设为默认";
        return switchViewCell;
    }
    else{
        LXTextFieldCell * cell = [tableView dequeueReusableCellWithIdentifier:@"asasasas"];
        if(!cell){
            cell = [[LXTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"asasasas"];
        }
        cell.delegate = self;
        [cell setTitle:titles[indexPath.row]];
        if(indexPath.row == 2){
           AreaPickView * pick =  [[AreaPickView alloc] init];
            pick.PickCallBack=^(NSString* text){
                cell.textField.text = text;
            };
            cell.textField.inputView = pick;
            cell.textField.placeholder = @"请选择";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            pick.pickDelegate = self;
        }
        [cell addRadius:0];
        cell.enableUnderLine = NO;
        cell.backgroundImageView.transform = CGAffineTransformIdentity;
        if(indexPath.row == 0){
            cell.backgroundImage = [UIImage imageNamed:@"brokerage_bg02"];
            cell.backgroundImageView.transform = CGAffineTransformRotate(cell.backgroundImageView.transform, M_PI);
            cell.enableUnderLine = YES;
        }else if (indexPath.row ==titles.count-1){
            cell.backgroundImage = [UIImage imageNamed:@"brokerage_bg02"];
        }else{
            cell.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth-2*globalMargin, cell.cellHeight)];
            cell.enableUnderLine = YES;
        }
        return cell;
    }

    return nil;
}

-(void)LXTextFieldCell:(LXTextFieldCell *)cell DidEndEditing:(NSString *)text{
    if([cell.title isEqualToString:@"收货人"]){
        [self.receiptAddressDic setValue:text forKey:@"receiver"];
        self.address.name = text;
    }
    if([cell.title isEqualToString:@"联系电话"]){
        [self.receiptAddressDic setValue:text forKey:@"phone"];
        self.address.mobile = text;
    }
    if([cell.title isEqualToString:@"所在地址"]){
        [self.receiptAddressDic setValue:text forKey:@"address"];
    }if([cell.title isEqualToString:@"请填写详细地址"]){
        [self.receiptAddressDic setValue:text forKey:@"detailAddress"];
        self.address.district = text;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}


-(void)AreaPickView:(AreaPickView *)pickkView didPickProvince:(NSString *)province city:(NSString *)city{
    self.address.province = province;
    self.address.city = city;
}

-(NSMutableDictionary*)receiptAddressDic{
    if(_receiptAddressDic == nil){
        _receiptAddressDic = [NSMutableDictionary dictionary];
    }
    return _receiptAddressDic;
}

@end
